<template>
	<view>
		<view class="personInfo" v-if="hasPersonInfo">
			<view class="title1">
				个人展示
			</view>
			<view class="personInfoTagWp" v-if="personInfoTags && personInfoTags.length>0">
				<view class="tagItemWp" v-for="(item, index) in personInfoTags" :key="index">
					<view class="tagItem">
						{{item.name}}
					</view>
				</view>
			</view>
			<view class="richTextWp">
				<textarea auto-height :value='introduction' disabled :maxlength="200"
					style="background:#fff;padding: 20rpx 0;color:#333;width: 100%; " />
			</view>
			<view class="title1" v-if="myImageList && myImageList.length>0">
				照片墙
			</view>
			<view class="water-container" v-if="myImageList && myImageList.length>0">
				<waterFallByCss :list="myImageList"></waterFallByCss>
				<!-- 	<view class="left">
					<image v-for="(item,index) in leftList" @click="previewImage(item)" :key="index" :src="item" alt=""
						mode="widthFix"></image>
				</view>
				<view class="right">
					<image v-for="(item,index) in rightList" @click="previewImage(item)" :key="index" :src="item" alt=""
						mode="widthFix"></image>
				</view> -->
			</view>
		</view>
		<view v-else>
			<nodata nodataImgUrl="https://file.casugn.com/storecompcard/temp/空页面@2x(1)_1681113483838.png"
				nodataText="请先添加个人信息"></nodata>
		</view>
		<view class="btnWp" @click="editPersonInfo" v-if="hasPersonInfo">
			<view class="btn" hover-class="hoverClass">
				<u-icon name="edit-pen" color="#fff" size="30"></u-icon>
				<text class="text" style="color: #fff;">编辑信息</text>
			</view>
		</view>
		<view class="btnWp" @click="addPersonInfo" v-else>
			<view class="btn" hover-class="hoverClass">
				<u-icon name="plus" color="#fff" size="30"></u-icon><text class="text" style="color: #fff;">新增信息</text>
			</view>
		</view>
	</view>
</template>

<script>
	import waterFallByCss from '@/pagesCard/components/waterfall/waterFallByCss.vue'
	import waterFall from '@/pagesCard/components/waterfall/waterfall.vue'
	export default {
		components: {
			waterFallByCss,
			waterFall
		},
		data() {
			return {
				userInfo: {},
				myImageList: [],
				personInfoTags: [],
				hasPersonInfo: null,
				cardId: '',
				introduction: '',
				id: '',
				// 初始化左右盒子
				leftList: [],
				rightList: [],
			}
		},
		onLoad(opt) {
			uni.showLoading({
				title: '加载中'
			})
			if (opt.cardId) {
				this.cardId = opt.cardId
			}

		},
		onShow() {
			this.leftList = []
			this.rightList = []
			this.queryCardIntroductionById(this.cardId)
		},
		methods: {
			async queryCardIntroductionById(cardId) {

				let res = await this.$api.queryCardIntroductionById({
					cardId
				})
				if (res.success) {
					if (!res.result) return
					this.id = res.result.id
					this.myImageList = []
					let myImageList = res.result.imageList || []
					if (myImageList.indexOf(',') == -1) {
						if (!myImageList || myImageList == '') {
							this.myImageList = []
						} else {
							let arr = []
							arr[0] = myImageList
							this.myImageList = arr
						}
					} else {
						this.myImageList = myImageList.split(',')

					}
					let bcCardLabelList = res.result.bcCardLabelList || []
					if (bcCardLabelList.length > 0) {
						this.personInfoTags = bcCardLabelList.filter(item =>
							item.isDel != 1)
					} else {
						this.personInfoTags = []
					}
					this.introduction = res.result.introduction || ''
					if (!this.myImageList && !this.introduction && !this.personInfoTags) {
						this.hasPersonInfo = false
					} else {
						this.hasPersonInfo = true
					}
				} else {
					this.hasPersonInfo = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				}
				// if (this.myImageList.length > 0) {
				// 	this.setDataList(this.myImageList, null, 0)
				// }


			},
			async setDataList(arr = [], imgKey, minHeight = 10) {
				let height1 = 0, //第一组图片累计高度
					height2 = 0, //第二组图片累计高度
					that = this
				for (let i = 0; i < arr.length; i++) {
					try {
						//等图片全部加载完成后再计算高度,否则因图片加载时间不一致出现每次重新加载图片 顺序错乱问题
						let heightData = await uni.getImageInfo({
							src: imgKey ? arr[i][imgKey] : arr[i]
						});
						let ImgHeight = heightData.length == 1 ? minHeight : heightData[1].height
						console.log(heightData, 'uni.getImageInfo-heightData');
						if (height1 <= height2) {
							height1 += ImgHeight
							that.leftList.push(arr[i])
						} else {
							height2 += ImgHeight
							that.rightList.push(arr[i])
						}
						uni.hideLoading()
					} catch (err) {
						console.error('getImageInfo-err', err)
					}
				}
			},
			previewImage(i) {
				if (i && this.myImageList.length > 0) {
					uni.previewImage({
						urls: this.myImageList,
						current: i
					})
				}
			},
			addPersonInfo() {
				uni.navigateTo({
					url: '/pagesCard/myProfile/addMyProfile?cardId=' + this.cardId
				})
			},
			editPersonInfo() {
				uni.navigateTo({
					url: `/pagesCard/myProfile/editMyProfile?cardId=${this.cardId}&id=${this.id}`
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.btnWp {
		width: 100%;
		height: 88rpx;
		position: fixed;
		bottom: 60rpx;
		left: 0;
		border: none;

		.btn {
			height: 100%;
			margin: 16rpx 48rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: $u-primary;

			.text {
				margin-left: 20upx;
				font-size: 30upx;
				font-weight: 400;
				width: 150rpx;
				height: 42rpx;
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 42rpx;
			}
		}
	}

	.personInfo {
		overflow: auto;

		.title1 {
			margin-left: 32rpx;
			height: 96rpx;
			font-size: 32rpx;
			font-weight: 600;
			color: #1D2129;
			line-height: 96rpx;
		}

		.richTextWp {
			margin: 24rpx 32rpx 0 32rpx;
		}

		.personInfoTagWp {
			margin: 0 32rpx;
			display: flex;
			align-items: center;
			flex-wrap: wrap;


			.tagItemWp {
				margin-top: 32rpx;
				height: 48rpx;
				margin-right: 24rpx;
				background-color: #E9F0FD;

				.tagItem {
					padding: 0 24rpx;
					text-align: center;
					height: 48rpx;
					font-size: 24rpx;
					font-weight: 400;
					color: #276AEE;
					line-height: 48rpx;

				}


			}
		}

		.title {
			letter-spacing: -2upx;
			font-size: 36upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #000000;
			line-height: 36upx;
			padding-bottom: 40upx;
		}

		.infoWp {

			.info {
				font-size: 30upx;
				font-weight: 400;
				color: #333333;
				line-height: 40upx;
			}
		}
	}

	.water-container {
		padding: 0 30rpx;
		font-size: 14rpx;
		line-height: 24rpx;

		.right,
		.left {
			display: inline-block;
			width: 49%;
			vertical-align: top;
		}

		.left {
			margin-right: 2%;
		}

		.left image,
		.right image {
			width: 100%;
			margin-bottom: 10rpx;
		}
	}
</style>